package com.tarena.lbs.basic.web.service.impl;

import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.basic.web.repository.DictRepository;
import com.tarena.lbs.basic.web.service.IDictService;
import com.tarena.lbs.common.basic.enums.DictStatusEnum;
import com.tarena.lbs.pojo.basic.bo.DictBO;
import com.tarena.lbs.pojo.basic.dos.DictDO;
import com.tarena.lbs.pojo.basic.param.DictParam;
import com.tarena.lbs.pojo.basic.query.DictQuery;
import com.tarena.lbs.pojo.basic.vo.DictVO;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

@Service
public class DictServiceImpl implements IDictService {

    @Autowired
    private DictRepository dictRepository;

    @Override
    public void save(DictParam dictParam) throws BusinessException {
        String dictType = dictParam.getDictType();
        DictQuery dictQuery = new DictQuery();
        dictQuery.setDictType(dictType);
         List<DictDO> dictBOS = dictRepository.selectByCondition(dictQuery);
        if(!CollectionUtils.isEmpty(dictBOS)){
            throw new BusinessException("10013","字典类型不能重复");
        }
        DictDO dictDO = new DictDO();
        BeanUtils.copyProperties(dictParam, dictDO);
        Date date = new Date();
        //设置创建时间以及更新时间
        dictDO.setCreateTime(date);
        dictDO.setUpdateTime(date);
        dictRepository.insert(dictDO);
    }

    @Override
    public void delete(Integer id) {
        dictRepository.delete(id);
    }

    @Override
    public void update(Integer id, DictParam dictParam) throws BusinessException {
        //修改字典类型不能重复
        DictQuery dictQuery = new DictQuery();
        dictQuery.setId(id);
        dictQuery.setDictType(dictParam.getDictType());
        dictQuery.setSelf("no");
        List<DictDO> dictBOS = dictRepository.selectByCondition(dictQuery);
        if(!CollectionUtils.isEmpty(dictBOS)){
            throw new BusinessException("10013","字典类型不能重复");
        }
        DictDO dictDO = new DictDO();
        BeanUtils.copyProperties(dictParam, dictDO);
        dictDO.setId(id);
        dictDO.setUpdateTime(new Date());
        dictRepository.update(dictDO);
    }

    @Override
    public DictBO detail(Integer id) {
        DictBO dictBO = dictRepository.detail(id);
        if (null == dictBO) {
            return null;
        }
        return dictBO;
    }

    @Override
    public PageResult<DictVO> list(DictQuery dictQuery) {

        List<Integer> statusList = new ArrayList<>();
        if (null == dictQuery.getStatus()) {
            statusList.add(DictStatusEnum.ACTIVE.getCode());
            statusList.add(DictStatusEnum.NOT_ACTIVE.getCode());
        } else {
            statusList.add(dictQuery.getStatus());
        }
       dictQuery.setStatusList(statusList);
        //加入分页
        int page = dictQuery.getPageNo();
        int pageSize = dictQuery.getPageSize();
        //new PageQuery(page, pageSize);
        List<DictDO> dictDOList = dictRepository.selectByCondition(dictQuery);
        PageInfo<DictDO> pageInfo = new PageInfo<>(dictDOList);
        List<DictVO> dictVOList = pageInfo.getList().stream().map(DictDO -> {
            DictVO DictVO = new DictVO();
            BeanUtils.copyProperties(DictDO, DictVO);
            return DictVO;
        }).collect(Collectors.toList());

        PageResult<DictVO> pageResult = new PageResult<>();
        pageResult.setObjects(dictVOList);
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setPageNo(page);
        pageResult.setPageSize(pageSize);
        return pageResult;
    }
}
